" vim: ft=vim
" repr()
"
Execute(Can hook into __repr__());
let cls = {}
function! cls.__repr__()
  return '<repr>'
endfunction

AssertEqual object#repr(cls), '<repr>'


Execute(Fail back on string() when object has no __repr__());
let input = [
      \ ['1', 1],
      \ ["''", ''],
      \ [ "{'name': 1}", {'name': 1}],
      \ ['[]', []],
      \ ['{}', {}],
      \ [ "'string'", 'string'],
      \ ]

if object#util#has_special_variables()
  call add(input, ['v:none', v:none])
endif

for [should, x] in input
  AssertEqual should, object#repr(x)
endfor


Execute(Throw WrongType when __repr__ is not a Funcref);
let cls = { '__repr__': [] }

AssertThrows call object#repr(g:cls)
Assert g:vader_exception=~#'ERROR(WrongType)'


Execute(Throw WrongType when __repr__() returns something not String);
let cls = {}
function! cls.__repr__()
  return []
endfunction

AssertThrows call object#repr(g:cls)
Assert g:vader_exception=~#'ERROR(WrongType)'


Execute(Expand list);
let input = [
      \ range(10),
      \ split(repeat('a ', 10)),
      \ map(range(10), 'function("empty")'),
      \]
for x in input
  AssertEqual string(x), object#repr(x)
endfor


Execute(Expand dict);
let input = [
      \ {'foo': 'foo'},
      \ {'foo': 1},
      \ {'foo': function('empty')},
      \ {'foo': 1, 'bar': v:false, 'sam': ''},
      \]
for x in input
  AssertEqual string(x), object#repr(x)
endfor
